<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="showAddRoleDialog(1,'0')">添加权限</el-button>
        </div>
        <el-table v-loading="isLoading" border :data="permissionList" row-key="id" default-expand-all>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" size="small" type="text" @click="showAddRoleDialog(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" :disabled="isDisabled" @click="agetPermissionDetail(row.id)">修改</el-button>
              <el-button size="small" type="text" :disabled="isDisabled" @click="delPremission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="title" :close-on-click-modal="false" @close="showDialog = false">
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="value"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            >
              />
            </el-switch></el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="addPermission">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionListApi, addPermissionApi, delPermissionApi, updatePermissionApi, getPermissionDetailApi } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [],
      isLoading: false,
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      value: '0',
      title: '',
      isDisabled: true // 控制按钮是否禁用
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      this.isLoading = true
      const { data } = await getPermissionListApi()
      this.permissionList = transListToTreeData(data, '0')
      this.isLoading = false
      // console.log(this.permissionList)
    },
    // 新增权限
    showAddRoleDialog(type, pid) {
      this.title = '新增权限'
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    addPermission() {
      // if (this.formData.name === '' || this.formData.code === '') {
      //   this.$message.error('权限名称和权限标识不能为空')
      //   return
      // }
      if (this.title === '修改权限') {
        // 编辑操作
        updatePermissionApi(this.formData)
      } else {
        // 添加操作
        addPermissionApi(this.formData)
        this.$message.success('操作成功')
      }

      this.getPermissionList()
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    // 删除权限
    delPremission(id) {
      this.$confirm('亲！您确定要删除吗', '温馨提示').then(async() => {
        await delPermissionApi(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      })
    },
    // 获取权限详情
    async agetPermissionDetail(id) {
      this.title = '修改权限'
      this.showDialog = true
      const { data } = await getPermissionDetailApi(id)
      this.formData = data
    },
    // 更新权限
    async updatePremission(id) {
      const res = await updatePermissionApi(id)
      console.log(res)
    }
  }
}
</script>
